<?php

namespace IdaasClient\Utils;


use Grpc\Base\Idaas\V1\IdaasClient;
use Hyperf\Context\Context;
use Hyperf\Grpc\StatusCode;
use Hyperf\GrpcServer\Exception\GrpcException;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Utils\ApplicationContext;
use Hyperfx\Utils\GenerateId;
use IdaasClient\Constants\IdaasConst;
use Psr\Http\Message\ServerRequestInterface;

class IdaasUserUtil {
    public static function getUserInfo() {
        $userInfo = Context::get(IdaasConst::USERINFO_CONTEXT_KEY);
        if (empty($userInfo)) {
            throw new GrpcException('此用户未登录', StatusCode::UNAUTHENTICATED);
        }
        return $userInfo;
    }

    /**
     * 获取当前用户所属的加盟商ID
     */
    public static function getOwnerPartnerId(): int {
        $userinfo = self::getUserInfo();
        if (empty($userinfo['partner_id'])) {
            throw new GrpcException('当前用户未指定加盟商', StatusCode::UNAUTHENTICATED);
        }
        return (int) $userinfo['partner_id'];
    }

    /**
     * 获取当前用户登录的加盟商
     */
    public static function getCurrentPartnerId(): int {
        $userinfo = self::getUserInfo();
        if (empty($userinfo['current_partner_id'])) {
            throw new GrpcException('当前用户未指定加盟商', StatusCode::UNAUTHENTICATED);
        }
        return (int) $userinfo['current_partner_id'];
    }

    public static function getDeptIds() {
        return Context::get(IdaasConst::DEPTIDS_CONTEXT_KEY, []);
    }

    public static function getShopId(): int {
        return Context::getOrSet(IdaasConst::SHOPID_CONTEXT_KEY, function () {
            // 获取用户信息
            $userInfo = self::getUserInfo();
            if (!isset($userInfo['user_type'])) {
                throw new GrpcException('用户类型错误,请重新登录', StatusCode::INVALID_ARGUMENT);
            }
            if (0 === strcmp($userInfo['user_type'], IdaasConst::USER_TYPE_SHOP)) {
                $shopId = (int) $userInfo['bind_data_id'];
            } else {
                throw new GrpcException('用户类型异常，请重新登录', StatusCode::INVALID_ARGUMENT);
            }
            if ($shopId < 1) {
                throw new GrpcException('门店ID错误，请重新登录', StatusCode::INVALID_ARGUMENT);
            }
            return $shopId;
        });
    }

    public static function getSupplierId(): int {
        return Context::getOrSet(IdaasConst::SUPPLIERID_CONTEXT_KEY, function () {
            // 获取用户信息
            $userInfo = self::getUserInfo();
            if (!isset($userInfo['user_type'])) {
                throw new GrpcException('用户类型错误,请重新登录', StatusCode::INVALID_ARGUMENT);
            }
            if (0 === strcmp($userInfo['user_type'], IdaasConst::USER_TYPE_SUPP)) {
                $supplierId = (int) $userInfo['bind_data_id'];
            } else if (0 === strcmp($userInfo['user_type'], IdaasConst::USERR_TYPE_COMM)) {
                $supplierId = ApplicationContext::getContainer()->get(IdaasClientUtil::class)->getSwitchedSupplier($userInfo['pool_id'], $userInfo['current_partner_id'], $userInfo['user_id'], $userInfo['session_id'] ?? '');
            } else {
                throw new GrpcException('用户类型异常，请重新登录', StatusCode::INVALID_ARGUMENT);
            }
            if ($supplierId < 1) {
                throw new GrpcException('供应商ID错误，请重新登录', StatusCode::INVALID_ARGUMENT);
            }
            return $supplierId;
        });
    }
}